package main

import (
	"flag"
	"fmt"
	"github.com/garyburd/redigo/redis"
	"os"
)

var RedisConn redis.Conn

func init() {
	RedisConn = NewRedis()
}
func main() {
	var mqType string
	flag.StringVar(&mqType, "mq_type", "producer", "请输入生产者[producer]或消费者[consumer]")
	flag.Parse()

	//conn:=NewRedis()
	//fmt.Println(conn)
	//fmt.Println(mqType)
	//redisWrite()
	redisGet()

	// 关闭redis连接
	defer RedisConn.Close()
}

type RedisConf struct {
	netType  string
	host     string
	password string
}

func NewRedis() redis.Conn {
	r := RedisConf{netType: "tcp", host: "39.100.194.133:6379", password: "jwp123456"}
	// 建立连接
	conn, err := redis.Dial(r.netType, r.host)
	if err != nil {
		fmt.Println("redis.Dial err=", err)
		os.Exit(1)
	}
	//输入密码
	conn.Send("auth", r.password)
	// 关闭连接
	//defer conn.Close() //函数结束时就调用了，所以不能在这里关闭
	return conn
}

//===========================================
func redisWrite() {
	/*// 建立连接
	conn, err := redis.Dial("tcp", "39.100.194.133:6379")
	if err != nil {
		fmt.Println("redis.Dial err=", err)
		return
	}
	//输入密码
	conn.Send("auth","jwp123456")*/

	//conn:=NewRedis()
	// 关闭连接
	//defer conn.Close()

	// 通过go向redis写入数据 string [key - value]
	_, err := RedisConn.Do("Set", "name", "Tom")
	if err != nil {
		fmt.Println("set err=", err)
		return
	}

}
func redisGet() {
	// 读取数据 获取名字
	myname, err := redis.String(RedisConn.Do("Get", "name"))
	if err != nil {
		fmt.Println("get err=", err)
		return
	}
	//nameString := r.(string)
	fmt.Println("Manipulate success, the name is", myname)
}
